<template>
    <van-nav-bar title="京东注册" left-text="返回" left-arrow @click-left="onClickLeft" />
    <van-form @submit="onSubmit">
        <van-cell-group inset>
            <van-field v-model="tel" name="用户名" label="请输入手机号"
                :rules="[{ required: true,validator, message: '请输入正确格式' }]" />
        </van-cell-group>
        <div style="margin: 16px;">
            <van-button  :disabled="!flag" block type="danger" native-type="submit">
                下一步
            </van-button>
        </div>
    </van-form>
</template>

<script setup lang="ts">
    import { useRouter } from 'vue-router';
    import { showConfirmDialog } from 'vant';
    import {ref} from "vue"
    import {checkPhone} from "@/service/login"
    let router = useRouter()
    const onClickLeft = ()=>{
        router.push("/login")
    }
    let tel = ref("")
    let flag = ref(false)
    const validator = (val:any) =>{
         return flag.value = /^1[3-9]\d{9}$/.test(val)
    }
    const onSubmit = ()=>{
        checkPhone(tel.value).then((res:any)=>{
            if (res.code === "200") {
                    showConfirmDialog({
                        title: '提示',
                        message:
                            `将向手机号${tel.value}发送验证码`,
                    })
                        .then(() => {
                            sessionStorage.setItem("tel",tel.value)
                           router.push("/regist/step2")
                        })
                }else{
                    showConfirmDialog({
                        message:
                            "号码已注册，是否去登录",
                    })
                        .then(() => {
                            router.push("/login")
                        })
                }
        })
    }
</script>

<style scoped>

</style>